package com.lkm.market2_0.m_imple.steelyardserver.server.cmd_handle;

import android.os.Handler;
import android.os.Looper;

import com.google.gson.Gson;
import com.lkm.frame.P;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.entity.BreedTo;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.help.SteelardHelp;
import com.lkm.market2_0.help.UIP;
import com.lkm.market2_0.m.steelyarddata.OrderEntity;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyardserver.OnSteelyardKeyEvent;
import com.lkm.market2_0.m.steelyardserver.SteelyardServerModule;
import com.lkm.market2_0.m.steelyardserver.to.WeightChangeInfo;
import com.lkm.market2_0.m_imple.steelyardserver.WeightChangeHelp;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsReceiverDataHandle;
import com.lkm.market2_0.m_imple.steelyardserver.server.CMDEntity;
import com.lkm.market2_0.m_imple.steelyardserver.server.ICMDHandle;
import com.lkm.market2_0.netapi.Api;




public class CMDBtnModifyHandle implements ICMDHandle {
    SteelyardDataModule mSteelyardDataModule;
    SteelyardServerModule mSteelyardServerModule;

    Handler handler = null;

    public CMDBtnModifyHandle() {
        handler = new Handler(Looper.getMainLooper());
        this.mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);
        this.mSteelyardServerModule= ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
    }

    @Override
    public AbsReceiverDataHandle handle(CMDEntity cmdEntity) {

        if(this.mSteelyardDataModule==null)
        this.mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);

        if(this.mSteelyardServerModule==null)
        this.mSteelyardServerModule= ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);

        SteelyardInfo steelyardInfo = mSteelyardDataModule.getSteelyardInfo(cmdEntity.getSteelyardMac());

        WeightChangeInfo weightChangeInfo= WeightChangeHelp.createByData(steelyardInfo, cmdEntity.getNum(),cmdEntity.getData());


        if(weightChangeInfo==null){
            P.p("======收到的weightChangeInfo为null");
            return null;
        }
        if(steelyardInfo == null){
            return null;
        }


       int goodIndex = weightChangeInfo.goodNum-1;

        BreedTo breedTo = null;
        try {
            breedTo = steelyardInfo.getBreedTo().get(goodIndex);
        } catch (Exception e) {
        }
        if (breedTo != null) {
            breedTo.price = weightChangeInfo.getUnitPrice();
            breedTo.goods_name = weightChangeInfo.getName();
        }
        
        ExpandConfig.instance.getTaskExecutor().execute(() -> {
            Api.server_breed_price(steelyardInfo.steelyardMac, weightChangeInfo.goodNum, weightChangeInfo.getUnitPrice(), null);
        });

        return null;
    }







}
